package com.xiayuanxing.flowable.service.impl;

import com.xiayuanxing.flowable.service.IFlowService;
import org.apache.commons.io.FileUtils;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * @projectName: flowable
 * @className: IFlowServiceImpl
 * @author: xiayuanxing
 * @description: TODO
 * @date: 2022/2/9 15:45
 * @version: 1.0
 */
@Service
public class IFlowServiceImpl implements IFlowService {


    @Autowired
    private RepositoryService repositoryService;


    /**
     * @param mFile:
     * @return Deployment
     * @author xiayuanxing
     * @description 建工作流（以文件导入方式创建）
     * @date 2022/2/9 15:45
     */
    @Override
    public Deployment createFlow(MultipartFile mFile, String bpmnKey) {


        //解析BPMN模型看是否成功
        XMLStreamReader reader = null;
        InputStream inputStream = null;
        try {
            File file = new File(mFile.getOriginalFilename());
            FileUtils.copyInputStreamToFile(mFile.getInputStream(), file);

            BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
            XMLInputFactory factory = XMLInputFactory.newInstance();
            inputStream = new FileInputStream(file);
            reader = factory.createXMLStreamReader(inputStream);
            BpmnModel model = bpmnXMLConverter.convertToBpmnModel(reader);
            List<Process> processes = model.getProcesses();
            Process process = null;
            if (CollectionUtils.isEmpty(processes)) {
                System.out.println("BPMN模型没有配置流程");
                return null;
            }
            process = processes.get(0);
            inputStream = new FileInputStream(file);
            DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().name(bpmnKey).addInputStream(process.getName(), inputStream);
            Deployment deployment = deploymentBuilder.deploy();
            // 会在本地产生临时文件，用完后需要删除
            if (file.exists()) {
                file.delete();
            }
            System.out.println("部署流程 name：" + process.getName() + " " + deployment);
            return deployment;
        } catch (Exception e) {
            System.out.println("BPMN模型创建流程异常" + e.getMessage());
            return null;
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
                System.out.println("关闭异常");
            }
        }


    }
}
